package com.witmore.skyline.web.controller;

import com.jk.aliyun.oss.JkOssClientCredentials;
import com.witmore.skyline.web.controller.vo.result.ResponseResult;
import com.witmore.skyline.web.service.third.constants.ApiClientCommonConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static java.lang.String.format;

/**
 * 文件上传接口
 */
@Slf4j
@RestController
@RequestMapping("/api/oss")
public class OSSFileController {

    @Autowired
    private JkOssClientCredentials jkOssClientCredentials;

    /**
     * 获取 OSS 上传所需的临时凭证
     */
    @GetMapping(value = "/getUploadCredentials", name = "获取 OSS 上传所需的临时凭证")
    public ResponseResult<Map<String, String>> getUploadCredentials(
            @RequestParam("cameraSn") String cameraSn) {
        String prefix = format(ApiClientCommonConstant.OSS_FILE_UPLOAD_LOCAL_PREFIX, cameraSn);
        Map<String, String> uploadCredentials = new HashMap<>();
        try {
            uploadCredentials = jkOssClientCredentials.getUploadCredentials(prefix);
        } catch (Exception e) {
            // 处理异常，返回错误信息给前端
            uploadCredentials.put("error", "获取临时凭证失败: " + e.getLocalizedMessage());
        }
        return ResponseResult.success(uploadCredentials);
    }

}
